package wangbaguanlixitong.dao;



import wangbaguanlixitong.model.Admin;

import java.sql.*;

public class AdminDao {


    //获取账号和密码
    public Admin selectUser(String username,String password) {
        Admin admin= new Admin();
        Connection conn = null;
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        try {
            String sql = "select * from t_admin where userName=? and userPW=?";
            conn = Dbutil.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, username);
            pstmt.setString(2, password);

            rs = pstmt.executeQuery();
            while(rs.next()){
                admin.setUserName(rs.getString(2));
                admin.setUserPw(rs.getString(3));

            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return admin;

    }

    /**
     * 用户密码修改
     */
    public  void GetReset(Admin admin){

        Connection conn = null;
        PreparedStatement sta = null;
        try {
            String sql = "update t_admin set userPw='"+admin.getUserPw()+"' where  userName ='"+admin.getUserName()+"'";
            conn = Dbutil.getConnection();
            sta = conn.prepareStatement(sql);
//            String sql = "update t_admin set userPw='"+admin.getUserPw()+"' where  userName ='"+admin.getUserName()+"'";
            sta.executeUpdate();
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            Dbutil.release(sta,conn);
        }
    }

//    //修改密码
//    public void updatepw(Admin admin){
//        Connection conn = null;
//        PreparedStatement ps = null;
//
//
//        try {
//            String sql = "update t_admin set userPw=? where userId =?";
//            conn = Dbutil.getConnection();
//            ps = conn.prepareStatement(sql);
//
//            ps.setString(1,admin.getUserPw());
//            ps.setString(2,admin.getUserName());
//
//            ps.executeUpdate();
//
//
//        } catch (ClassNotFoundException | SQLException e) {
//            e.printStackTrace();
//        }finally {
//            Dbutil.release(ps,conn);
//        }
//
//    }
//
//    //获取Admin的username
//    public Admin getAdminName(String name){
//
//        Admin admin =null;
//        Connection conn = null;
//        PreparedStatement ps = null;
//        ResultSet rs = null;
//        try {
//            String sql = "select * from t_admin where userName = ?";
//            conn = Dbutil.getConnection();
//            ps = conn.prepareStatement(sql);
//            ps.setString(1,name);
//            rs = ps.executeQuery();
//
//            while(rs.next()){
//                admin = new Admin();
//                admin.setUserName(rs.getString("userName"));
//                admin.setUserPw(rs.getString("userPw"));
//            }
//
//
//        } catch (ClassNotFoundException | SQLException e) {
//            e.printStackTrace();
//        } finally {
//            Dbutil.release(ps,conn);
//        }
//
//
//        return admin;
//    }



}
